package com.ad.pet.app.recharge;

import android.app.Activity;

import com.ad.pet.api.ApiService;
import com.ad.pet.app.RxDisposable;
import com.ad.pet.common.data.http.AppSubscriber;
import com.ad.pet.common.data.injector.PerActivity;
import com.ad.pet.common.structure.mvp.AbsBasePresenter;
import com.ad.pet.models.remote.User;
import com.ad.pet.models.remote.account.CountDrawTotal;
import com.ad.pet.models.remote.recharge.WithDraw;

import javax.inject.Inject;

/**
 * Created by SEELE on 2019/6/3.
 */

@PerActivity
public class CashMoneyPresenter extends AbsBasePresenter implements CashMoneyContracts.Presenter {

    private ApiService mApiService;
    private CashMoneyContracts.View mView;

    private Activity mActivity;

    private RxDisposable mRxDisposable;

    @Inject
    public CashMoneyPresenter(ApiService apiService,
                              Activity activity, RxDisposable rxDisposable) {
        mApiService = apiService;
        mActivity = activity;
        mRxDisposable = rxDisposable;
    }

    @Override
    public void onCreate(CashMoneyContracts.View view) {
        this.mView = view;
    }


    @Override
    public void onDestroy() {
        if (mRxDisposable != null)
            mRxDisposable.clear();
        if (mActivity != null) mActivity = null;
        mView = null;
    }


    @Override
    public void getWithDraw() {
        mRxDisposable.add(mApiService.getUserWithdrawWindow().subscribe(new AppSubscriber<WithDraw>(mView) {
            @Override
            public void next(WithDraw productDetail) {
                mView.getWithDrawSuccess(productDetail);
            }
        }));
    }

    @Override
    public void addWithDraw(String type, String no, String name, String amount) {
        mRxDisposable.add(mApiService.addWithdrawAmount(type, no, name, amount).subscribe(new AppSubscriber<User>(mView) {
            @Override
            public void next(User productDetail) {
                mView.addWithDrawSuccess();
            }
        }));
    }

    @Override
    public void getCurrentTotalWithdrawal() {
        mRxDisposable.add(mApiService.getCurrentTotalWithdrawal().subscribe(new AppSubscriber<CountDrawTotal>(mView) {
            @Override
            public void next(CountDrawTotal productDetail) {
                mView.getCurrentTotalWithdrawalSuccess(productDetail);
            }
        }));
    }
}
